// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.util.list;

import zombie.util.AbstractIntCollection;
import zombie.util.IntCollection;
import zombie.util.IntIterator;
import zombie.util.hash.DefaultIntHashFunction;
import zombie.util.util.Exceptions;

public abstract class AbstractIntList extends AbstractIntCollection implements IntList {
    protected AbstractIntList() {
    }

    @Override
    public boolean add(int v) {
        this.add(this.size(), v);
        return true;
    }

    @Override
    public void add(int index, int v) {
        Exceptions.unsupported("add");
    }

    @Override
    public boolean addAll(int index, IntCollection c) {
        if (index < 0 || index > this.size()) {
            Exceptions.indexOutOfBounds(index, 0, this.size());
        }

        IntIterator intIterator = c.iterator();

        boolean _boolean;
        for (_boolean = intIterator.hasNext(); intIterator.hasNext(); index++) {
            this.add(index, intIterator.next());
        }

        return _boolean;
    }

    @Override
    public int indexOf(int c) {
        return this.indexOf(0, c);
    }

    @Override
    public int indexOf(int index, int c) {
        IntListIterator intListIterator = this.listIterator(index);

        while (intListIterator.hasNext()) {
            if (intListIterator.next() == c) {
                return intListIterator.previousIndex();
            }
        }

        return -1;
    }

    @Override
    public IntIterator iterator() {
        return this.listIterator();
    }

    @Override
    public int lastIndexOf(int c) {
        IntListIterator intListIterator = this.listIterator(this.size());

        while (intListIterator.hasPrevious()) {
            if (intListIterator.previous() == c) {
                return intListIterator.nextIndex();
            }
        }

        return -1;
    }

    @Override
    public int lastIndexOf(int index, int c) {
        IntListIterator intListIterator = this.listIterator(index);

        while (intListIterator.hasPrevious()) {
            if (intListIterator.previous() == c) {
                return intListIterator.nextIndex();
            }
        }

        return -1;
    }

    @Override
    public IntListIterator listIterator() {
        return this.listIterator(0);
    }

    @Override
    public IntListIterator listIterator(final int index) {
        if (index < 0 || index > this.size()) {
            Exceptions.indexOutOfBounds(index, 0, this.size());
        }

        return new IntListIterator() {
            private int ptr = index;
            private int lptr = -1;

            @Override
            public boolean hasNext() {
                return this.ptr < AbstractIntList.this.size();
            }

            @Override
            public int next() {
                if (this.ptr == AbstractIntList.this.size()) {
                    Exceptions.endOfIterator();
                }

                this.lptr = this.ptr++;
                return AbstractIntList.this.get(this.lptr);
            }

            @Override
            public void remove() {
                if (this.lptr == -1) {
                    Exceptions.noElementToRemove();
                }

                AbstractIntList.this.removeElementAt(this.lptr);
                if (this.lptr < this.ptr) {
                    this.ptr--;
                }

                this.lptr = -1;
            }

            @Override
            public void add(int _int) {
                AbstractIntList.this.add(this.ptr++, _int);
                this.lptr = -1;
            }

            @Override
            public boolean hasPrevious() {
                return this.ptr > 0;
            }

            @Override
            public int nextIndex() {
                return this.ptr;
            }

            @Override
            public int previous() {
                if (this.ptr == 0) {
                    Exceptions.startOfIterator();
                }

                this.ptr--;
                this.lptr = this.ptr;
                return AbstractIntList.this.get(this.ptr);
            }

            @Override
            public int previousIndex() {
                return this.ptr - 1;
            }

            @Override
            public void set(int _int) {
                if (this.lptr == -1) {
                    Exceptions.noElementToSet();
                }

                AbstractIntList.this.set(this.lptr, _int);
            }
        };
    }

    @Override
    public int removeElementAt(int index) {
        Exceptions.unsupported("removeElementAt");
        throw new RuntimeException();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (!(obj instanceof IntList)) {
            return false;
        } else {
            IntListIterator intListIterator0 = this.listIterator();
            IntListIterator intListIterator1 = ((IntList)obj).listIterator();

            while (intListIterator0.hasNext() && intListIterator1.hasNext()) {
                if (intListIterator0.next() != intListIterator1.next()) {
                    return false;
                }
            }

            return !intListIterator0.hasNext() && !intListIterator1.hasNext();
        }
    }

    @Override
    public int hashCode() {
        int _int = 1;
        IntIterator intIterator = this.iterator();

        while (intIterator.hasNext()) {
            _int = 31 * _int + DefaultIntHashFunction.INSTANCE.hash(intIterator.next());
        }

        return _int;
    }
}
